package com.maimao.goods.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.Result;
import com.maimao.goods.client.EsClient;
import com.maimao.goods.controller.base.BaseController;
import com.maimao.goods.service.GoodsCheckService;
import com.maimao.model.goods.log.GoodsCheckInput;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * @author MaoLin Wang
 * @date 2021/3/23 2:57 下午
 */
@RestController
@RequestMapping("/goods/goodsCheck")
public class GoodsCheckController extends BaseController {

    @Autowired
    private EsClient esClient;

    @Autowired
    private GoodsCheckService goodsCheckService;


    /**
     * 查询当前人的待审核商品记录
     */
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('goods-manager')")
    public Result<?> list(@RequestBody GoodsCheckInput checkInput) {
        AuthUser authUser = checkAndGetAuthUser();
        checkInput.setSysUserId(authUser.getId());
        return esClient.list(checkInput);
    }

    /**
     * 商品审核 通过-> 修改es审核记录状态（1 通过 2 未通过）并更新审核时间和商品状态 (1有效/已审核,未通过不修改)
     *
     * @return
     */
    @PostMapping("/check")
    @PreAuthorize("hasAuthority('goods-manager')")
    public Result<?> check(@Validated @RequestBody GoodsCheckInput input) {
        if (Long.valueOf(input.getId()) < 1 || Long.valueOf(input.getGoodsId()) < 0) {
            return Result.notValid();
        }
        goodsCheckService.check(input.getId(), input.getGoodsId(), input.getOk(), input.getFailReason());
        return Result.ok("审核成功");
    }
}
